package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("EventqualitytypeDAO")
@Transactional
public class EventqualitytypeDAOImpl extends AbstractJpaDao<Eventqualitytype> implements EventqualitytypeDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Eventqualitytype.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public EventqualitytypeDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeById(Integer id) throws DataAccessException {

		return findEventqualitytypeById(id, -1, -1);
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeById(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventqualitytypeById", startResult, maxRows, id);
			return (Eventqualitytype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByName(String name) throws DataAccessException {

		return findEventqualitytypeByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventqualitytypeByName", startResult, maxRows, name);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByNameContaining(String name) throws DataAccessException {

		return findEventqualitytypeByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventqualitytypeByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDegree(String degree) throws DataAccessException {

		return findEventqualitytypeByDegree(degree, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDegree(String degree, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventqualitytypeByDegree", startResult, maxRows, degree);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDegreeContaining(String degree) throws DataAccessException {

		return findEventqualitytypeByDegreeContaining(degree, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDegreeContaining(String degree, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventqualitytypeByDegreeContaining", startResult, maxRows, degree);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDescription(String description) throws DataAccessException {

		return findEventqualitytypeByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findEventqualitytypeByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDescriptionContaining(String description)
			throws DataAccessException {

		return findEventqualitytypeByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByDescriptionContaining(String description, int startResult,
			int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findEventqualitytypeByDescriptionContaining", startResult, maxRows,
				description);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeByPrimaryKey(Integer id) throws DataAccessException {
		return findEventqualitytypeByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventqualitytypeByPrimaryKey", startResult, maxRows, id);
			return (Eventqualitytype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventqualitytypesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Eventqualitytype> findAllEventqualitytypes() throws DataAccessException {

		return findAllEventqualitytypes(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findAllEventqualitytypes(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllEventqualitytypes", startResult, maxRows);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventqualitytype> findEventqualitytypesByEvent(Integer eventId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllEventqualitytypesByEvent", start, max, eventId);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Eventqualitytype> findEventqualitytypesByQualitytype(Integer qualitytypeId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllEventqualitytypesByQualitytype", start, max, qualitytypeId);
		return new LinkedHashSet<Eventqualitytype>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Eventqualitytype> findEventqualitytypeByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Eventqualitytype>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Eventqualitytype> findEventqualitytypesByDetachedCriteria(CmiPagination page)
			throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Eventqualitytype.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Eventqualitytype> eventqualitytypes = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return eventqualitytypes;
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeByPhysicalPrimaryKey(Integer eventId, Integer qualitytypeId,
			String degree) throws DataAccessException {
		return findEventqualitytypeByPhysicalPrimaryKey(eventId, qualitytypeId, degree, -1, -1);
	}

	@Transactional
	public Eventqualitytype findEventqualitytypeByPhysicalPrimaryKey(Integer eventId, Integer qualitytypeId,
			String degree, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findEventqualitytypeByPhyscialPrimaryKey", startResult, maxRows, eventId,
					qualitytypeId, degree);
			return (Eventqualitytype) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Eventqualitytype entity) {
		return true;
	}
}
